package com.example.testcomposeproject.pages.sideEffect.testDisposableEffect

import androidx.activity.OnBackPressedCallback
import androidx.activity.OnBackPressedDispatcher
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material3.Switch
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.hilt.navigation.compose.hiltViewModel
import com.example.testcomposeproject.MyLogger
import com.example.testcomposeproject.PreViewData
import com.example.testcomposeproject.pages.copy.CopyViewModel
import com.example.testcomposeproject.ui.theme.AppTheme

/**
 * @author: hemin
 * @date: 2024/6/21
 * @Desc: 这个思路可以包装高德地图的MapView
 *
 */
@Preview(showBackground = true, device = PreViewData.PreviewPhoneSize)
@Composable
fun PreViewTestDisposableEffectUI(modifier: Modifier = Modifier) {
    AppTheme {
//        TestDisposableEffectUI(modifier = modifier)

    }
}

/**
 * viewModel 用这个方法
 */
/*@Composable
fun TestDisposableEffectScreen(
    modifier: Modifier = Modifier,
    viewModel: CopyViewModel = hiltViewModel()
) {
    TestDisposableEffectUI(modifier)
}*/

/**
 * 真正的UI元素
 */
@Composable
fun TestDisposableEffectUI(   backDispatcher:OnBackPressedDispatcher,) {

    DisposableEffect(Unit) {
        onDispose {
           MyLogger.hLog().e("DisposableEffect销毁")
        }
    }

    var isCheck by remember { mutableStateOf(false) }
    Column(
        modifier = Modifier.fillMaxSize(),
        horizontalAlignment = androidx.compose.ui.Alignment.CenterHorizontally,
        verticalArrangement = androidx.compose.foundation.layout.Arrangement.Center
    ) {


        Row (modifier = Modifier.fillMaxWidth(),
            horizontalArrangement = androidx.compose.foundation.layout.Arrangement.SpaceEvenly
        ){
            Switch(checked = isCheck, onCheckedChange = {
                isCheck = it
            } )

            Text(text = if (isCheck) "Add Back CallBack" else "No Add Back CallBack" )

        }

        if(isCheck){
            BackHandler(backDispatcher){
                println("back")
            }
        }
    }

}

@Composable
fun BackHandler(
   backDispatcher:OnBackPressedDispatcher,
   onBack:()->Unit
) {
    //代码1
/*    val backCallback = object : OnBackPressedCallback(true) {
        override fun handleOnBackPressed() {
            onBack()
        }
    }*/



    //优化代码1
     //外面可以改变onBack的值
    val updateOnBack by rememberUpdatedState(onBack)
    //缓存对象不用每次都创建
    val backCallback  by remember {
        mutableStateOf(
            object : OnBackPressedCallback(true) {
                override fun handleOnBackPressed() {
                    updateOnBack()
                }
            }
        )
    }

    DisposableEffect(Unit) {
        MyLogger.hLog().e("开关-->开")
        backDispatcher.addCallback(backCallback)
        onDispose {
               MyLogger.hLog().e("开关-->关")
            backCallback.remove()
        }
    }

    Text(text = "AddBackPress")

}


